package cn.net.wanji.entity;

import cn.net.wanji.utils.StringUtils;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import static cn.net.wanji.utils.TimeUtils.getDateFormatType;

/**
 * @author guodejun
 * @createTime 2022/10/28 10:38
 * @description
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class JsonRoot {

    private String recordId;

    private String recordType;

    @JsonProperty(value = "ERID")
    private String ERID;
    /**
     * 全景相机轨迹id
     */
    @JsonProperty(value = "CarId")
    private String CarId;

    @JsonProperty(value = "vehiclePicInfos")
    private List<VehiclePicInfo> vehiclePicInfos;

    @JsonProperty(value = "RSUInfos")
    private List<RSUInfo> RSUInfos;

    private String unlawfulMold;
    private String detectTime;
    private String unlawfulCredible;

    public VehicleRsuInfo getVehicleRsuInfo(String fileNginx) {
        VehicleRsuInfo vehicleRsuInfo = new VehicleRsuInfo();
        vehicleRsuInfo.setDetectTime(StringUtils.timeRevise(detectTime));
        vehicleRsuInfo.setRecordId(recordId);
        vehicleRsuInfo.setRecordType(recordType);
        vehicleRsuInfo.setErId(ERID);
        vehicleRsuInfo.setUnlawfulMold(unlawfulMold);
        vehicleRsuInfo.setConfidenceDegree(unlawfulCredible);
        vehicleRsuInfo.setCarId(CarId);


        if (vehiclePicInfos != null && vehiclePicInfos.size() > 0) {
            VehiclePicInfo vehiclePicInfo = vehiclePicInfos.get(0);
            vehiclePicInfo.updateVehicleRsuInfo(vehicleRsuInfo);
        }

        if (RSUInfos != null && RSUInfos.size() > 0) {
            RSUInfo rsuInfo = RSUInfos.get(0);
            rsuInfo.updateVehicleRsuInfo(vehicleRsuInfo);
        }

        // 计算 车牌号 缩写
        String licenseCode = vehicleRsuInfo.getLicenseCode();
        if (!StringUtils.isEmpty(licenseCode)) {
            String licenseAbbre = licenseCode.substring(0, 1);
            vehicleRsuInfo.setLicenseAbbre(licenseAbbre);
        }

        // 计算图片存放地址
        // 获取detectTime中的年月日 时分秒
        LocalDateTime dateTime = LocalDateTime.now();
        String dateFormatType = getDateFormatType(detectTime);
        if (dateFormatType != null) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormatType);
            // 解析时间字符串
            dateTime = LocalDateTime.parse(detectTime, formatter);
        }

        // 获取当前年、月、日
        int year = dateTime.getYear();
        int month = dateTime.getMonthValue();
        int day = dateTime.getDayOfMonth();
        // 格式化月份为两位数显示
        String formattedMonth = String.format("%02d", month);
        // 拼接文件路径
        String filePath = File.separator + year + formattedMonth + File.separator + day + File.separator
                + recordId + File.separator;
        vehicleRsuInfo.setHeadImage(fileNginx + filePath + "0.jpeg");
        vehicleRsuInfo.setTailImage(fileNginx + filePath + "1.jpeg");
        vehicleRsuInfo.setSideImage(fileNginx + filePath + "2.jpeg");
        vehicleRsuInfo.setLicenseImage(fileNginx + filePath + "3.jpeg");
        vehicleRsuInfo.setVideo(fileNginx + filePath + "4.mp4");
        vehicleRsuInfo.setCompositeImage(fileNginx + filePath + "5.jpeg");
        return vehicleRsuInfo;
    }

}
